package com.wangtao.firstapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.HomeVPAdapter;
import com.wangtao.firstapp.fragments.original_author_fragments.ColectonFragment;
import com.wangtao.firstapp.fragments.original_author_fragments.PublishFragment;
import com.wangtao.firstapp.modles.OriginalAuthorHead;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户作者  介绍页面
 * Created by lanouhn on 16/9/7.
 */
public class OriginalAuthorActivity extends FragmentActivity {

    private SharedPreferences sp;

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> tabNames;
    private List<Fragment> tabFragments;
    private HomeVPAdapter homeVPAdapter;
    // private OriginalAuthorAdapter originalAuthorAdapter;

    private TextView name, number, like, attention, fans;
    private ImageButton back;
    private ImageView logo;
    private Button guanzhu, yiguanzhu;

    //用户作者 介绍页面（Head 喜欢，关注，粉丝，，）
    private String urlHead;
    private String paramsHead;
    private OriginalAuthorHead originalAuthorHead;

    //用户作者  介绍页面（收藏—>单品，文章）


    //用户作者  介绍页面（发布—>，文章，晒单）

    //    //用户作者  介绍页面（收藏—>单品，文章）
//    private String urlCollect = "http://open4.bantangapp.com/users/center/collect";
//    private String paramsCollect = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id=1&page=0&pagesize=20";

//    //用户作者  介绍页面（发布—>，文章，晒单）
//    private String urlPublish="http://open4.bantangapp.com/users/center/publish";
//    private String paramsPublish="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id=1&page=0&pagesize=18";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_original_author);
        initView();
        initData();
        getDataFromWeb();
        getDataFromWeb1();
        initEvent();
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout_act_original_author);
        viewPager = (ViewPager) findViewById(R.id.viewPager_act_original_author);

        back = (ImageButton) findViewById(R.id.back_act_original_author);
        name = (TextView) findViewById(R.id.name_act_original_author);
        number = (TextView) findViewById(R.id.number_act_original_author);
        logo = (ImageView) findViewById(R.id.logo_act_original_author);
        like = (TextView) findViewById(R.id.like_act_original_author);
        attention = (TextView) findViewById(R.id.attention_act_original_author);
        fans = (TextView) findViewById(R.id.fans_act_original_author);
        guanzhu = (Button) findViewById(R.id.guanzhu_act_original_author);
        yiguanzhu = (Button) findViewById(R.id.yiguanzhu_act_original_author);
    }

    private void initData() {

        tabNames = new ArrayList<>();
        tabNames.add("收藏");
        tabNames.add("发布");

        tabFragments = new ArrayList<>();
        ColectonFragment colectonFragment = new ColectonFragment();
        tabFragments.add(colectonFragment);
        PublishFragment publishFragment = new PublishFragment();
        tabFragments.add(publishFragment);

        homeVPAdapter = new HomeVPAdapter(getSupportFragmentManager(), tabNames, tabFragments);
        viewPager.setAdapter(homeVPAdapter);
        tabLayout.setupWithViewPager(viewPager);


    }

    private void getDataFromWeb() {
        //打开文件
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        //接收作者ID
        String author_ID = sp.getString("author_iD", "2466368");

        //拼接用户作者 介绍页面（Head 喜欢，关注，粉丝，，）接口
        final String urlHead = "http://open4.bantangapp.com/users/center/userInfo";
        final String paramsHead = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id=" + author_ID;


        try {
            HttpUtils.doPostAsyn(urlHead, paramsHead, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_Head = new Gson();
                    originalAuthorHead = gson_Head.fromJson(result, OriginalAuthorHead.class);
                    if (originalAuthorHead != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    private void getDataFromWeb1() {
        Intent intent = getIntent();
        String userid = intent.getStringExtra("userid");

        //拼接用户作者 介绍页面（Head 喜欢，关注，粉丝，，）接口
        final String urlHead = "http://open4.bantangapp.com/users/center/userInfo";
        final String paramsHead = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id=" + userid;


        try {
            HttpUtils.doPostAsyn(urlHead, paramsHead, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_Head = new Gson();
                    originalAuthorHead = gson_Head.fromJson(result, OriginalAuthorHead.class);
                    if (originalAuthorHead != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    name.setText(originalAuthorHead.getData().getNickname());
                    number.setText(originalAuthorHead.getData().getUser_sign() + "");

                    if (originalAuthorHead.getData().getUser_id().equals("1")) {
                        if (!originalAuthorHead.getData().getArticle_like().equals("")) {
                            like.setText(originalAuthorHead.getData().getArticle_like());
                        } else {
                            like.setText("0");
                        }
                    }else {
                        if (!originalAuthorHead.getData().getPost_like().equals("")){
                            like.setText(originalAuthorHead.getData().getPost_like());
                        }else {
                            like.setText("0");
                        }
                    }

                    if (!originalAuthorHead.getData().getAttentions().equals("")) {
                        attention.setText(originalAuthorHead.getData().getAttentions() + "");
                    } else {
                        attention.setText("0");
                    }
                    if (!originalAuthorHead.getData().getFans().equals("")) {
                        fans.setText(originalAuthorHead.getData().getFans());
                    } else {
                        fans.setText("0");
                    }


                    Picasso.with(OriginalAuthorActivity.this).load(originalAuthorHead.getData().getAvatar()).into(logo);
                    break;
            }
        }
    };

    //点击关注
    private void initEvent() {
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(OriginalAuthorActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                guanzhu.setVisibility(View.GONE);
                yiguanzhu.setVisibility(View.VISIBLE);
                String fansStr = originalAuthorHead.getData().getFans();
                Integer fansNum = Integer.valueOf(fansStr) + 1;
                String fansShow = String.valueOf(fansNum);
                fans.setText(fansShow);
            }
        });
        //点击取消关注
        yiguanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                guanzhu.setVisibility(View.VISIBLE);
                yiguanzhu.setVisibility(View.GONE);
                if (!originalAuthorHead.getData().getFans().equals("")) {
                    fans.setText(originalAuthorHead.getData().getFans());
                } else {
                    fans.setText("0");
                }
            }
        });

        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
}